package com.peng.community.service.impl;

import com.peng.community.constant.CommunityConstant;
import com.peng.community.entity.Comment;
import com.peng.community.entity.DiscussPost;
import com.peng.community.mapper.CommentMapper;
import com.peng.community.service.CommentService;
import com.peng.community.service.DiscussPostService;
import com.peng.community.util.SensitiveFilter;
import org.omg.CORBA.Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService, CommunityConstant {
    @Autowired
    CommentMapper commentMapper;

    @Override
    public List<Comment> findCommentsByEntity(int entityType, int entityId, int offset, int limit) {
        return commentMapper.findCommentsByEntity(entityType,entityId,offset,limit);
    }

    @Override
    public int findCountCommentsByEntity(int entityType, int entityId) {
        return commentMapper.findCountCommentsByEntity(entityType, entityId);
    }

    @Autowired
    SensitiveFilter sensitiveFilter;

    @Autowired
    DiscussPostService discussPostService;

    @Override
    public int addComment(Comment comment) {
        if (comment == null) {
            throw new IllegalArgumentException("参数不能为空");
        }
        //对标签转移，对敏感词进行过滤
        comment.setContent(sensitiveFilter.filter(HtmlUtils.htmlEscape(comment.getContent())));
        int added = commentMapper.insertComment(comment);

        //更新帖子数量（只有回复帖子的评论数量需要更新）
        if (comment.getEntityType() == ENTITY_TYPE_POST) {
            int count = commentMapper.findCountCommentsByEntity(ENTITY_TYPE_POST, comment.getEntityId());
            discussPostService.updateConmentCount(comment.getEntityId(), count);
        }

        return added;
    }

    @Override
    public Comment findCommentById(int id) {
        return commentMapper.findCommentsById(id);
    }


}
